package com.fxkj.cloud.drms.ui.controller;

import com.fxkj.cloud.drms.application.AlarmHistoryApplicationService;
import com.fxkj.cloud.drms.application.model.vo.AlarmHistoryModelView;
import com.fxkj.cloud.drms.application.model.vo.ProductAlarmStatisticsModelView;
import com.fxkj.common.controller.BaseController;
import com.fxkj.common.exception.FxException;
import com.fxkj.common.model.PageServerResponse;
import com.fxkj.common.model.ServerResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * <p>description: </p>
 * @author ww
 * @date 2025/4/7 15:32
 */
@RestController
@RequestMapping("api/v1/alarmHistory")
@Tag(name = "告警历史")
public class AlarmHistoryController extends BaseController {
    private final AlarmHistoryApplicationService alarmHistoryApplicationService;

    public AlarmHistoryController(AlarmHistoryApplicationService alarmHistoryApplicationService) {
        this.alarmHistoryApplicationService = alarmHistoryApplicationService;
    }

    @Operation(summary = "查询历史", description = "查询历史", parameters = {
            @Parameter(name = "page", description = "page", required = false),
            @Parameter(name = "size", description = "size", required = false),
            @Parameter(name = "deviceId", description = "设备id", required = false),
            @Parameter(name = "productId", description = "产品id", required = false),
            @Parameter(name = "alarmLevel", description = "告警级别", required = false),
            @Parameter(name = "startTs", description = "告警开始时间", required = false),
            @Parameter(name = "endTs", description = "告警结束时间", required = false),
            @Parameter(name = "alarmText", description = "告警内容", required = false),
            @Parameter(name = "closed", description = "是否关闭", required = false),
    })
    @GetMapping("list")
    public PageServerResponse<AlarmHistoryModelView> list(@RequestParam(name = "page", required = false,defaultValue = "0") Integer page,
                                                          @RequestParam(name = "size", required = false,defaultValue = "10") Integer size,
                                                          @RequestParam(name = "deviceId", required = false) UUID deviceId,
                                                          @RequestParam(name = "productId", required = false) UUID productId,
                                                          @RequestParam(name = "alarmLevel", required = false) Integer alarmLevel,
                                                          @RequestParam(name = "startTs", required = false) Long startTs,
                                                          @RequestParam(name = "endTs", required = false) Long endTs,
                                                          @RequestParam(name = "alarmText", required = false) String alarmText,
                                                          @RequestParam(name = "closed",required = false,defaultValue = "false") Boolean closed) throws FxException {
        UUID orgId = getOrgId();
        return PageServerResponse.success(alarmHistoryApplicationService.list(page, size, deviceId, productId, orgId, alarmLevel, startTs, endTs, alarmText, closed));
    }

    @Operation(summary = "产品告警统计", description = "产品告警统计", parameters = {
            @Parameter(name = "productId", description = "productId", required = false),
    })
    @GetMapping("product/alarm/statistics")
    public ServerResponse<ProductAlarmStatisticsModelView> productAlarmStatistics(@RequestParam(name = "productId", required = false) UUID productId) {
        return ServerResponse.success(alarmHistoryApplicationService.productAlarmStatistics(productId));
    }

    @Operation(summary = "产品模型告警统计", description = "产品模型告警统计", parameters = {
            @Parameter(name = "productModelId", description = "productModelId", required = false),
    })
    @GetMapping("productModel/alarm/statistics")
    public ServerResponse<ProductAlarmStatisticsModelView> productModelAlarmStatistics(@RequestParam(name = "productModelId", required = false) UUID productModelId) {
        return ServerResponse.success(alarmHistoryApplicationService.productModelAlarmStatistics(productModelId));
    }

}
